package com.carryduck.boss.controller;

import com.carryduck.util.JSONResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Yang on 2019/7/7.
 */
@Controller
@RequestMapping("/web")
public class WebController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @RequestMapping("/login")
    public String tologin(){
        logger.info("定向登陆页");
        return "login";
    }

    @RequestMapping("/logout")
    public String logout( HttpServletRequest request ){
        request.getSession().removeAttribute("user");
        logger.info("退出系统");
        return "redirect:login";
    }

    /**
     * 功能描述: 登入系统
     */
    @RequestMapping(value = "toLogin")
    @ResponseBody
    public Map<String,Object> login(HttpServletRequest request , String username , String password){
        Map<String,Object> data = new HashMap();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            data.put("code",201);
            data.put("msg","用户名或密码不能为空");
            return data;
        }
//        TbAdminUser adminUser = userAdminService.findUserAdmin(username, password);
//        if(null == adminUser){
//            data.put("code",201);
//            data.put("msg","用户名或密码错误");
//            return data;
//        }
//        request.getSession().setAttribute("user",adminUser );
//        data.put("code",200);
        data.put("url","/admin/index");
        data.put("msg","登录成功");
//        data.put("adminName",adminUser.getUserName());
        return data;
    }

    @RequestMapping(value = "/addSession")
    @ResponseBody
    public JSONResult addSession(HttpServletRequest request){
//        TbAdminUser user = new TbAdminUser();
//        request.getSession().setAttribute("user", user);
        return JSONResult.result(200, "保存session成功");
    }


}
